package com.zxp.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxp.server.config.Result;
import com.zxp.server.dto.BlogForm;
import com.zxp.server.dto.BlogInfo;
import com.zxp.server.dto.RespPageBean;
import com.zxp.server.service.IBlogService;
import com.zxp.server.util.token.annotation.CheckToken;
import com.zxp.server.util.token.tokenService;
import com.zxp.server.vo.Blog;
import com.zxp.server.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱小皮
 * @since 2022-05-14
 */
@Api(value = "博客管理", tags = "博客管理")
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private IBlogService blogService;

    @Autowired
    private tokenService tokenService;

    @CheckToken
    @ApiOperation(value = "发布博客")
    @PostMapping("/postBlog")
    public Result postBlog(@RequestBody BlogForm addBlog) {
        try {
            blogService.postBlog(addBlog);
        } catch (Exception exception) {
            exception.printStackTrace();
            return  Result.error("发布失败");
        }
        return  Result.success("发布成功");
    }

    @CheckToken
    @ApiOperation(value = "编辑博客")
    @PostMapping("/editBlog")
    public Result editBlog(@RequestBody BlogForm addBlog) {
        try {
            blogService.editBlog(addBlog);
        } catch (Exception exception) {
            exception.printStackTrace();
            return  Result.error("编辑失败");
        }
        return  Result.success("编辑成功");

    }

    @ApiOperation(value = "获取所有已发布的博客(含高级查询)")
    @GetMapping("/blogs")
    public Result getBlogs(@RequestParam(defaultValue = "1") Integer currentPage,
                           @RequestParam(defaultValue = "10") Integer size,Blog blog){
        RespPageBean blogs = blogService.getBlogInfos(currentPage, size,blog);
        return Result.success("获取博客列表成功",blogs);
    }

    @CheckToken
    @ApiOperation(value = "根据用户获取已发布博客")
    @GetMapping("/userBlogs1")
    public Result getReleaseBlogsByUser(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size, HttpServletRequest httpServletRequest){
        User user = tokenService.getUserByToken(httpServletRequest);
        RespPageBean blogs;
        if (user!=null){
             blogs = blogService.getReleaseBlogInfosByUser(currentPage, size,user);
             return Result.success("获取博客列表成功",blogs);
        }else {
            return Result.error("用户信息过期，请重新登录");
        }

    }

    @CheckToken
    @ApiOperation(value = "根据用户获取草稿博客(含高级查询）")
    @GetMapping("/userBlogs2")
    public Result getDraftBlogsByUser(@RequestParam(defaultValue = "1") Integer currentPage,
                                        @RequestParam(defaultValue = "10") Integer size,  Blog blog,
                                      HttpServletRequest httpServletRequest){
        User user = tokenService.getUserByToken(httpServletRequest);
        RespPageBean blogs;
        if (user!=null){
            blogs = blogService.getDraftBlogInfosByUser(currentPage, size,user,blog);
            return Result.success("获取博客(草稿)列表成功",blogs);
        }else {
            return Result.error("用户信息过期，请重新登录");
        }
    }


    @ApiOperation(value = "根据id获取博客")
    @GetMapping("/getBlogsData")
    public Result getBlogsData(@RequestParam Integer blogId){
        BlogInfo blogInfo = blogService.getBlogInfoById(blogId);
        blogService.addView(blogInfo);
        return Result.success("获取成功",blogInfo);
    }

    @ApiOperation(value = "根据id和用户删除博客")
    @GetMapping("/deleteBlog")
    public Result deleteBlogById(@RequestParam Integer blogId,HttpServletRequest httpServletRequest){
        //用户只能删除属于自己的博客
        User user = tokenService.getUserByToken(httpServletRequest);
        if (user==null){
            return Result.error("用户信息过期，请重新登录");
        }
        blogService.deleteBlogById(blogId,user.getId());
        return Result.success("删除成功");
    }


}
